package com.jincheng.parser;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.jincheng.model.Team;

import android.util.Xml;

public class XMLParser {
	public static List<Team> XML2Object(String xmlStr) throws Exception {
		InputStream in = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
		List<Team> teams = null;
		Team team = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();
		while (type != parser.END_DOCUMENT) {
			switch (type) {
			case XmlPullParser.START_TAG:
				if ("teams".equals(parser.getName())) {
					teams = new ArrayList<Team>();
				} else if ("team".equals(parser.getName())) {
					team = new Team();
				} else if ("id".equals(parser.getName())) {
					String id = parser.nextText();
					team.setId(id);
				} else if ("link".equals(parser.getName())) {
					String link = parser.nextText();
					team.setLink(link);
				} else if ("large_image_url".equals(parser.getName())) {
					String largeImageUrl = parser.nextText();
					team.setLargeImageUrl(largeImageUrl);
				} else if ("small_image_url".equals(parser.getName())) {
					String smallImageUrl = parser.nextText();
					team.setSmallImageUrl(smallImageUrl);
				} else if ("title".equals(parser.getName())) {
					String title = parser.nextText();
					team.setTitle(title);
				} else if ("product".equals(parser.getName())) {
					String product = parser.nextText();
					team.setProduct(product);
				} else if ("team_price".equals(parser.getName())) {
					String teamPrice = parser.nextText();
					team.setTeamPrice(teamPrice);
				} else if ("market_price".equals(parser.getName())) {
					String marketPrice = parser.nextText();
					team.setMarketPrice(marketPrice);
				} else if ("rebate".equals(parser.getName())) {
					String rebate = parser.nextText();
					team.setRebate(rebate);
				} else if ("start_date".equals(parser.getName())) {
					String startDate = parser.nextText();
					team.setStartDate(startDate);
				} else if ("end_date".equals(parser.getName())) {
					String endDate = parser.nextText();
					team.setEndDate(endDate);
				} else if ("state".equals(parser.getName())) {
					String state = parser.nextText();
					team.setState(state);
				} else if ("tipped".equals(parser.getName())) {
					String tipped = parser.nextText();
					team.setTipped(tipped);
				}  else if ("tipping_point".equals(parser.getName())) {
					String tippingPoint = parser.nextText();
					team.setTippingPoint(tippingPoint);
				} else if ("current_point".equals(parser.getName())) {
					String currentPoint = parser.nextText();
					team.setCurrentPoint(currentPoint);
				} else if ("city".equals(parser.getName())) {
					String city = parser.nextText();
					team.setCity(city);
				} else if ("group".equals(parser.getName())) {
					String group = parser.nextText();
					team.setGroup(group);
				}
				break;
			case XmlPullParser.END_TAG:
				if ("team".equals(parser.getName())) {
					teams.add(team);
					team = null;
				}
				break;
			}
			type = parser.next();
		}
		in.close();
		return teams;
	}
}
